#ifndef _SC_TIMER_H_
#define _SC_TIMER_H_

#include "..\Global.h"

// #define PSA_SEL_TIMER0_DIV2()    do{OPTION_REG &= 0b11110000; OPTION_REG |= 0b00001000;}while(0)
// #define PSA_SEL_TIMER0_DIV4()    do{OPTION_REG &= 0b11110000; OPTION_REG |= 0b00001001;}while(0)
// #define PSA_SEL_TIMER0_DIV8()    do{OPTION_REG &= 0b11110000; OPTION_REG |= 0b00001010;}while(0)
// #define PSA_SEL_TIMER0_DIV16()   do{

#define OPTION_REG_INTEG_SEL_RISE    0b01000000      //---- 选择上升沿触发中断
#define OPTION_REG_INTEG_SEL_FALL    0b00000000      //---- 选择下降沿触发中断
#define OPTION_REG_TM0_CLK_SEL_EXT   0b00100000      //---- 选择外部时钟
#define OPTION_REG_TM0_CLK_SEL_INT   0b00000000      //---- 选择内部时钟
#define OPTION_REG_TM0_CLK_FALL      0b00000000      //---- T0CKI引脚上升沿递增
#define OPTION_REG_TM0_CLK_RISE      0b00010000      //---- T0CKI引脚下降沿递增
#define OPTION_REG_PSA_SEL_TM0       0b00000000      //---- 预分频器分配给TIMER0
#define OPTION_REG_PSA_SEL_WDT       0b00001000      //---- 预分频器分配给WDT
#define OPTION_REG_DIV2_WDT_16MS     0b00000000      //---- TIMER0 预分频/2   WDT-16ms
#define OPTION_REG_DIV4_WDT_32MS     0b00000001      //---- TIMER0 预分频/4   WDT-32ms
#define OPTION_REG_DIV8_WDT_64MS     0b00000000      //---- TIMER0 预分频/8   WDT-64ms
#define OPTION_REG_DIV16_WDT_128MS   0b00000000      //---- TIMER0 预分频/16  WDT-128ms
#define OPTION_REG_DIV32_WDT_256MS   0b00000000      //---- TIMER0 预分频/32  WDT-256ms
#define OPTION_REG_DIV64_WDT_512MS   0b00000000      //---- TIMER0 预分频/64  WDT-512ms
#define OPTION_REG_DIV128_WDT_1024MS 0b00000000      //---- TIMER0 预分频/128 WDT-1024ms
#define OPTION_REG_DIV256_WDT_2048MS 0b00000000      //---- TIMER0 预分频/256 WDT-2048ms

#define T1CON_T1GINV_LOW             0b00000000      //---- 门控信号低电平计数
#define T1CON_T1GINV_HIGH            0b10000000      //---- 门控信号高电平计数
#define T1CON_GE_ON                  0b01000000      //---- TIMER1计数由TIMER1门控功能控制
#define T1CON_GE_OFF                 0b00000000      //---- TIMER1始终计数
#define T1CON_T1CKPS_8               0b00110000      //---- TIMER1时钟分频1:8
#define T1CON_T1CKPS_4               0b00100000      //---- TIMER1时钟分频1:4
#define T1CON_T1CKPS_2               0b00010000      //---- TIMER1时钟分频1:2
#define T1CON_T1CKPS_1               0b00000000      //---- TIMER1时钟分频1:1
#define T1CON_T1OSCEN_OFF            0b00000000      //---- LP振荡器关闭
#define T1CON_T1OSCEN_ON             0b00001000      //---- 使能LP振荡器作为TIMER1时钟源
#define T1CON_T1SYNC_OFF             0b00000100      //---- TIMER1不与外部时钟同步
#define T1CON_T1SYNC_ON              0b00000000      //---- TIMER1与外部时钟输入同步
#define T1CON_TMR1CS_LP              0b00000010      //---- TIMER1时钟源来自LP振荡器或T1CKI引脚
#define T1CON_TMR1CS_INT             0b00000000      //---- TIMER1时钟源来自内部时钟源

#define T2CON_CLK_SEL_INT            0b00000000      //---- TIMER2时钟源选择内部时钟
#define T2CON_CLK_SEL_EXT            0b10000000      //---- TIMER2时钟源选择外部时钟
#define T2CON_TOUTPS_1               0b00000000      //---- TIMER2输出后分频选择1/1
#define T2CON_TOUTPS_2               0b00001000      //---- TIMER2输出后分频选择1/2
#define T2CON_TOUTPS_3               0b00010000      //---- TIMER2输出后分频选择1/3
#define T2CON_TOUTPS_4               0b00011000      //---- TIMER2输出后分频选择1/4
#define T2CON_TOUTPS_5               0b00100000      //---- TIMER2输出后分频选择1/5
#define T2CON_TOUTPS_6               0b00101000      //---- TIMER2输出后分频选择1/6
#define T2CON_TOUTPS_7               0b00110000      //---- TIMER2输出后分频选择1/7
#define T2CON_TOUTPS_8               0b00111000      //---- TIMER2输出后分频选择1/8
#define T2CON_TOUTPS_9               0b01000000      //---- TIMER2输出后分频选择1/9
#define T2CON_TOUTPS_10              0b01001000      //---- TIMER2输出后分频选择1/1
#define T2CON_TOUTPS_11              0b01010000      //---- TIMER2输出后分频选择1/11
#define T2CON_TOUTPS_12              0b01011000      //---- TIMER2输出后分频选择1/12
#define T2CON_TOUTPS_13              0b01100000      //---- TIMER2输出后分频选择1/13
#define T2CON_TOUTPS_14              0b01101000      //---- TIMER2输出后分频选择1/14
#define T2CON_TOUTPS_15              0b01110000      //---- TIMER2输出后分频选择1/15
#define T2CON_TOUTPS_16              0b01111000      //---- TIMER2输出后分频选择1/16
#define T2CON_T2CKPS_1               0b00000000      //---- TIMER2预分频选择1/1
#define T2CON_T2CKPS_4               0b00000001      //---- TIMER2预分频选择1/4
#define T2CON_T2CKPS_16              0b00000010      //---- TIMER2预分频选择1/16

#define SEL_WDT_16MS()      do{OPTION_REG = OPTION_REG_PSA_SEL_WDT + OPTION_REG_DIV2_WDT_16MS;}while(0)
#define SEL_WDT_32MS()      do{OPTION_REG = OPTION_REG_PSA_SEL_WDT + OPTION_REG_DIV4_WDT_32MS;}while(0)
#define SEL_WDT_64MS()      do{OPTION_REG = OPTION_REG_PSA_SEL_WDT + OPTION_REG_DIV8_WDT_64MS;}while(0)
#define SEL_WDT_128MS()     do{OPTION_REG = OPTION_REG_PSA_SEL_WDT + OPTION_REG_DIV16_WDT_128MS;}while(0)
#define SEL_WDT_256MS()     do{OPTION_REG = OPTION_REG_PSA_SEL_WDT + OPTION_REG_DIV32_WDT_256MS;}while(0)
#define SEL_WDT_512MS()     do{OPTION_REG = OPTION_REG_PSA_SEL_WDT + OPTION_REG_DIV64_WDT_512MS;}while(0)
#define SEL_WDT_1024MS()    do{OPTION_REG = OPTION_REG_PSA_SEL_WDT + OPTION_REG_DIV128_WDT_1024MS;}while(0)
#define SEL_WDT_2048MS()    do{OPTION_REG = OPTION_REG_PSA_SEL_WDT + OPTION_REG_DIV256_WDT_2048MS;}while(0)

#define TMR1_EN()           do{TMR1ON=1;}while(0)
#define TMR1_DIS()          do{TMR1ON=0;}while(0)
#define TMR2_EN()           do{TMR2ON=1;}while(0)
#define TMR2_DIS()          do{TMR2ON=0;}while(0)
#define WDT_EN()            do{SWDTEN = 1;}while(0)
#define WDT_DIS()           do{SWDTEN = 0;}while(0)

void initTimer0(uint8 initValue);
void WaitTM0(uint8 tm);

void initTimer2(uint8 initValue);
void WaitTM2(uint8 tm);

#define WAIT_TIMER2()       do{ while(!TMR2IF); TMR2IF = 0;}while(0)

#endif
